//
//  ECUIView_Category.m
//  Edu_iOS
//
//  Created by yanyan on 2017/9/7.
//  Copyright © 2017年 yanyan. All rights reserved.
//


#import "ECUIView_Category.h"
//#import "CategoryModel.h"

#define ECCATEGORYBACKGROUNDCOLOR  [UIColor colorWithHexString:@"#f7f7f7"]
#define ECCATEGORYCELLBORDERCOLOR  [UIColor colorWithHexString:@"#dadcdb"]
@interface ECUITVCell_PrimaryCategory(){
    UIView *selectedView;

}
@end
@implementation ECUITVCell_PrimaryCategory
- (void)addSubviews
{
    [super addSubviews];
    self.contentView.backgroundColor = ECCATEGORYBACKGROUNDCOLOR;
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor colorWithHexString:@"#fafafa"];
    self.selectedBackgroundView = view;
    selectedView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 3, CGRectGetHeight(self.frame))];
    selectedView.backgroundColor = ECTHEMESELECTEDCOLOR;
    [self.selectedBackgroundView addSubview:selectedView];
    
    self.textLabel.font = [UIFont systemFontOfSize:ceil(12 * kScreenWidth / 320)];
    self.textLabel.adjustsFontSizeToFitWidth = YES;
    self.textLabel.textColor = [UIColor colorWithHexString:@"#333333"];
    self.textLabel.highlightedTextColor = [UIColor colorWithHexString:@"ff6700"];
    
}
- (void)setObject:(id)object
{
    [super setObject:object];
    SubCategoryModel *mo = object;
    self.textLabel.text = mo.title;
}
@end
@interface ECUITVCell_SubCategory(){
    
}
@end
@implementation ECUITVCell_SubCategory
- (void)addSubviews
{
    [super addSubviews];
    self.backgroundColor = [UIColor clearColor];
    self.contentView.backgroundColor = [UIColor clearColor];
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
    self.selectedBackgroundView = view;
    self.textLabel.font = [UIFont systemFontOfSize:ceil(12 * kScreenWidth / 320)];
    self.textLabel.adjustsFontSizeToFitWidth = YES;
    self.textLabel.textColor = [UIColor colorWithHexString:@"#333333"];
    self.textLabel.highlightedTextColor = ECTHEMESELECTEDCOLOR;
}
- (void)setObject:(id)object
{
    [super setObject:object];
    SubCategoryModel *mo = object;
    self.textLabel.text = mo.title;
}
@end
#pragma mark --    ---
// cv cell
@interface ECUICVCell_SubCategory()
{
}
@end
@implementation ECUICVCell_SubCategory
- (void)addSubviews
{
    [super addSubviews];
    self.contentView.backgroundColor = ECCATEGORYBACKGROUNDCOLOR;
    self.layer.cornerRadius = 3;
    self.layer.borderColor = [ECCATEGORYCELLBORDERCOLOR CGColor];
    self.layer.borderWidth = 0.5;
    self.layer.masksToBounds = YES;
    UIView *view = [[UIView alloc]initWithFrame:self.bounds];
    view.backgroundColor = ECTHEMESELECTEDCOLOR;
    self.selectedBackgroundView = view;
    UIView *conView = self.contentView;//[[UIView alloc]init];
//    [self.contentView addSubview:conView];
    _titleLabel = [[UILabel alloc]init];
    _titleLabel.textColor = [UIColor colorWithHexString:@"#595959"];
    _titleLabel.font = [UIFont systemFontOfSize:ceil(11*kPercentage)];
    [conView addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(conView);
        make.left.greaterThanOrEqualTo(self.mas_left);
        make.right.lessThanOrEqualTo(self.mas_right);
    }];
    
}
- (void)setSelected:(BOOL)selected
{
    [super setSelected:selected];
    if (selected) {
        self.contentView.backgroundColor = ECTHEMESELECTEDCOLOR;
        _titleLabel.textColor = [UIColor whiteColor];

    } else {
        _titleLabel.textColor = [UIColor colorWithHexString:@"#595959"];
        self.contentView.backgroundColor = ECCATEGORYBACKGROUNDCOLOR;
    }
}
- (void)setObject:(id)object
{
    [super setObject:object];
    PrimaryCategoryModel *mo = object;
    self.titleLabel.text = mo.title;
//    self.selected = mo.selected;
}

@end


// image cv cell
// cv cell
@interface ECUICVCell_FoldSubCategory()
{
    UIView *conView;
    UIImageView *imageView;
}
@end
@implementation ECUICVCell_FoldSubCategory
- (void)addSubviews
{
    [super addSubviews];
    self.contentView.backgroundColor = ECCATEGORYBACKGROUNDCOLOR;
    self.layer.cornerRadius = CGRectGetHeight(self.bounds)/2;
    self.layer.masksToBounds = YES;
    conView = [[UIView alloc]init];
    [self.contentView addSubview:conView];
    _titleLabel = [[UILabel alloc]init];
    _titleLabel.textColor = [UIColor colorWithRed:0.3725 green:0.3725 blue:0.3725 alpha:1];
    _titleLabel.font = [UIFont systemFontOfSize:ceil(11*kScreenWidth/320)];
    [conView addSubview:_titleLabel];
    if (!imageView) {
        imageView = [[UIImageView alloc]init];
        [conView addSubview:imageView];
    }
    [conView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.lessThanOrEqualTo(self);
        make.centerX.equalTo(self);
        make.centerY.equalTo(self);
    }];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(conView.mas_left);
        make.centerY.equalTo(conView.mas_centerY);
    }];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_titleLabel.mas_right).offset(2);
        make.width.equalTo(@(8));
        make.height.equalTo(@(5));
        make.centerY.equalTo(conView);
        make.right.equalTo(conView.mas_right);
        
        
    }];
    
}
- (void)setObject:(id)object
{
    [super setObject:object];
    SubCategoryModel *mo = object;
    if (mo.folderStyle != CategoryCellStyleNone) {
       
        if (mo.folderStyle == CategoryCellStyleFolded) {
            imageView.image = [UIImage imageNamed:@"edu_subcategory_more"];
        } else {
            imageView.image = [UIImage imageNamed:@"edu_subcategory_unfolder"];
        }
        
    }
    
    self.titleLabel.text = mo.title;
}

@end
@interface ECUICVHeader_Category(){

    
}
@end
@implementation ECUICVHeader_Category

-(void)setObject:(id)object
{
    [super setObject:object];
    if ([object isKindOfClass:[NSString class]]) {
        self.titleLabel.text = object;
    } else {
        self.titleLabel.text = [object valueForKey:@"name"];
    }
    
}

-(void)addSubviews
{
    [super addSubviews];
  
    _titleLabel = [[UILabel alloc]init];
    _titleLabel.font = [UIFont systemFontOfSize:ceil(13 * kPercentage)];
    _titleLabel.textColor = [UIColor colorWithHexString:@"#000000"];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_titleLabel];

    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.centerX.equalTo(self);                                         
    }];

}
@end
#import "ECModelData.h"
#import "CYUITVDSD_base.h"
#import "CYUITVDSDBridge.h"
#import "UIView+BorderLine.h"

@interface ECUIView_Category()<UITableViewDelegate,UITableViewDataSource>
{
    CYUITVDSD_base *dsd1;
    CYUITVDSD_base *dsd2;
}
@end
@implementation ECUIView_Category
@synthesize tableView = _tableView;
@synthesize secondView = _secondView;
@synthesize thirdView = _thirdView;
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        CGFloat tvWidth = (CGRectGetWidth(frame) - 30)/3;
        CGFloat tvHeight = CGRectGetHeight(frame);
        self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

        [self addSubview:self.tableView];
// 分割线
      
        _secondView = [[UITableView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.tableView.frame),0 , tvWidth+10, tvHeight) style:UITableViewStylePlain];
        _secondView.backgroundColor = [UIColor colorWithHexString:@"#fafafa"];
        _secondView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        _secondView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self addSubview:_secondView];
        
        dsd1 = [[CYUITVDSD_base alloc]init];
        dsd1.classesIdbyReusedCells = @{@"cell":@"ECUITVCell_SubCategory"};
        dsd1.selectionType = CYTableViewCell_SelectionTypeSingleSelection;
        WeakSelf(weakSelf);
        dsd1.block_itemAtSelectedIndexPath = ^(id obj, NSIndexPath *indexPath) {
        
            [weakSelf showThirdCategoryInSecondCategory:obj];

        };
        [CYUITVDSDBridge bridge:_secondView dsd:dsd1];
        
        //// 分割线2
        UIView *secondLine = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_secondView.frame), 0, 0.5, tvHeight)];
        secondLine.backgroundColor = [UIColor colorWithHexString:@"e5e5e5"];
        [self addSubview:secondLine];
        
        _thirdView = [[UITableView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(secondLine.frame),0 , tvWidth+20, tvHeight) style:UITableViewStylePlain];
        _thirdView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

        _thirdView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _thirdView.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
        [self addSubview:_thirdView];
        dsd2 = [[CYUITVDSD_base alloc]init];
        dsd2.hasSelectedStyle = YES;
        dsd2.classesIdbyReusedCells = @{@"cell":@"ECUITVCell_SubCategory"};
        dsd2.selectionType = CYTableViewCell_SelectionTypeSingleSelection;
        [CYUITVDSDBridge bridge:_thirdView dsd:dsd2];
        dsd2.block_itemAtSelectedIndexPath = ^(id obj, NSIndexPath *indexPath) {
            DLog(@"--进入列表页");
            [weakSelf showListVCWithCategory:obj];
        };
        self.layer.masksToBounds = YES;
    }
    return self;
}
-(UITableView *)tableView
{
    if (!_tableView) {
        CGFloat tvWidth = (CGRectGetWidth(self.frame) - 30)/3;
        CGFloat tvHeight = CGRectGetHeight(self.frame);
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, tvWidth, tvHeight)];
        _tableView.backgroundColor = [UIColor colorWithHexString:@"#f5f5f5"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        _tableView.backgroundColor = [UIColor colorWithRed:0.9451 green:0.9490 blue:0.9569 alpha:1];
        [_tableView registerClass:[ECUITVCell_PrimaryCategory class] forCellReuseIdentifier:@"firstCell"];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView borderForColor:[UIColor colorWithHexString:@"#e5e5e5"] borderWidth:0.5 borderType:UIBorderSideTypeRight];
//        UIView *firstLine = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_tableView.frame), 0, 0.5, tvHeight)];
//        firstLine.backgroundColor = [UIColor colorWithHexString:@"e5e5e5"];
//        [self addSubview:firstLine];
    }
    return _tableView;
}
- (UITableView *)secondView
{
    if (!_secondView) {
        _secondView = [[UITableView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.tableView.frame),0 , self.tableView.width+10, self.tableView.height) style:UITableViewStylePlain];
        _secondView.backgroundColor = [UIColor colorWithHexString:@"#fafafa"];
        _secondView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        _secondView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self addSubview:_secondView];
        
        dsd1 = [[CYUITVDSD_base alloc]init];
        dsd1.classesIdbyReusedCells = @{@"cell":@"ECUITVCell_SubCategory"};
        dsd1.selectionType = CYTableViewCell_SelectionTypeSingleSelection;
        WeakSelf(weakSelf);
        dsd1.block_itemAtSelectedIndexPath = ^(id obj, NSIndexPath *indexPath) {
            
            [weakSelf showThirdCategoryInSecondCategory:obj];
            
        };
        [CYUITVDSDBridge bridge:_secondView dsd:dsd1];
    }
    return _secondView;
}
-(void)showListVCWithCategory:(PrimaryCategoryModel *)mo
{
    if (self.block_selectedAtIndex) {
        self.block_selectedAtIndex(mo, nil);
    }
}
-(void)showThirdCategoryInSecondCategory:(PrimaryCategoryModel *)mo{

    dsd2.object = @{kTableCellDataSourceId:@[mo.subCategoryArray]};
    [_thirdView reloadData];
    if (mo.subCategoryArray.count < 1) {
        self.secondCategoryModel = nil;
        [self showListVCWithCategory:mo];
    } else {
        self.secondCategoryModel = mo;
    }
}
- (void)setCategorysData:(NSMutableArray *)categorysData
{
    _categorysData = categorysData;
    [self.tableView reloadData];
    if (!self.defaultCategoryModel) {
        _selectedIndex = 0;
        [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:_selectedIndex inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
        
        PrimaryCategoryModel *mo = [_categorysData objectAtIndex:_selectedIndex];
        
        _firstCategoryModel = mo;
        _secondCategoryModel = nil;
        if (_firstCategoryModel.modelId.length <= 0) {
            return;
        }
        [self showSelectedFirstCategory:mo];
    } else {
        for (int i = 0; i< self.categorysData.count; i ++) {
            PrimaryCategoryModel *mo = [self.categorysData objectAtIndex:i];
            //  一级分类
            if ([mo.modelId  isEqualToString:self.defaultCategoryModel.modelId]) {
                _selectedIndex = i;
                _firstCategoryModel = mo;
                _secondCategoryModel = nil;
                [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
                if (mo.subCategoryArray.count > 0) {
                    dsd1.object = @{kTableCellDataSourceId:@[mo.subCategoryArray]};
                    [_secondView reloadData];
                    [self.secondView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
                }
                return;
            } else {
                // 二级分类
                for (int j = 0; j< mo.subCategoryArray.count; j++) {
                    PrimaryCategoryModel *mo_second = [mo.subCategoryArray objectAtIndex:j];
                    if ([mo_second.modelId  isEqualToString:self.defaultCategoryModel.modelId]) {
                        _firstCategoryModel = mo;
                        _secondCategoryModel = mo_second;
                        dsd1.object = @{kTableCellDataSourceId:@[mo.subCategoryArray]};
                        [_secondView reloadData];
                        [self.secondView selectRowAtIndexPath:[NSIndexPath indexPathForRow:j inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
                        if (mo_second.subCategoryArray.count > 0) {
                            dsd2.object = @{kTableCellDataSourceId:@[mo_second.subCategoryArray]};
                            [_thirdView reloadData];
                            [self.thirdView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
                        }
                        [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
                        return;
                    } else {
                        // 三级分类
                        for (int k = 0; k< mo_second.subCategoryArray.count; k++) {
                            PrimaryCategoryModel *mo_third = [mo_second.subCategoryArray objectAtIndex:k];
                            
                            if ([mo_third.modelId  isEqualToString:self.defaultCategoryModel.modelId]) {
                                dsd1.object = @{kTableCellDataSourceId:@[mo.subCategoryArray]};
                                [_secondView reloadData];
                                dsd2.object = @{kTableCellDataSourceId:@[mo_second.subCategoryArray]};
                                [_thirdView reloadData];
                                
                                [self.thirdView selectRowAtIndexPath:[NSIndexPath indexPathForRow:k inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
                                
                                [self.secondView selectRowAtIndexPath:[NSIndexPath indexPathForRow:j inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
                                [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
                                return;
                            }
                        }
                    }
                    
                }
            }
        }
        
    }



    
}
- (void)setFirstCategoryModel:(PrimaryCategoryModel *)firstCategoryModel
{
    _firstCategoryModel = firstCategoryModel;
    for (int i = 0; i < self.categorysData.count; i++) {
        PrimaryCategoryModel *mo = [self.categorysData objectAtIndex:i];
        if ([mo.modelId  isEqualToString:_firstCategoryModel.modelId]) {
            _selectedIndex = i;
            [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
        }
        return;
    }
}
// 选中第一级分类，则展示第二级分类和第三级分类
-(void)showSelectedFirstCategory:(PrimaryCategoryModel *)mo{
    _firstCategoryModel = mo;
    self.secondCategoryModel = nil;
    dsd1.object = @{kTableCellDataSourceId:@[mo.subCategoryArray]};
    [_secondView reloadData];
    
    if ([mo.subCategoryArray count] >1) {
        [_secondView selectRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
        PrimaryCategoryModel *mo1 = [mo.subCategoryArray objectAtIndex:1];
        self.secondCategoryModel = mo1;

        [self showSelectedSecondCategory:mo1];
    } else {
        dsd2.object = @{kTableCellDataSourceId:@[]};
        [_thirdView reloadData];
    }
    if (mo.subCategoryArray.count <= 0) {
        [self showListVCWithCategory:mo];
    }
    
}
- (void)setDefaultCategoryModel:(PrimaryCategoryModel *)defaultCategoryModel
{
    _defaultCategoryModel = defaultCategoryModel;
    
}
// 选中第二级分类，则展示第三级分类

-(void)showSelectedSecondCategory:(PrimaryCategoryModel *)mo{
    // 如果 选中的是第二级分类，全部
    dsd2.object = @{kTableCellDataSourceId:@[mo.subCategoryArray]};
    [_thirdView reloadData];
    if (mo.subCategoryArray.count <= 0) {
        [self showListVCWithCategory:mo];
    }
}
#pragma mark -TableView Delegate -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.categorysData.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITVCell_base *cell = [tableView dequeueReusableCellWithIdentifier:@"firstCell" forIndexPath:indexPath];
    cell.object = [self.categorysData objectAtIndex: indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _selectedIndex = indexPath.row;
    PrimaryCategoryModel *mo = [self.categorysData objectAtIndex:_selectedIndex];
    [self showSelectedFirstCategory:mo];
    
}

@end
